/**
 * 数据字典分类管理
 */
DicManager = Ext.extend(Ext.Panel, {
	constructor : function(config) {
		Ext.applyIf(this, config);
		this.initUIComponents();
		DicManager.superclass.constructor.call(this, {
					id : 'DicManager',
					height : 450,
					//autoScroll : true,
					layout : 'border',
					title : '数据字典分类管理',
					iconCls : "menu-dictionary",
					items : [this.leftPanel, this.centerPanel]
				});
	},
	initUIComponents : function() {
		this.leftPanel = new Ext.ux.TreePanelEditor({
			id : 'dicType',
			height : 450,
			autoScroll : true,
			region : 'west',
			title : '数据字典分类',
			collapsible : true,
			split : true,
			border : false,
			width : 210,

			url : __ctxPath + '/system/treeGlobalType.do?catKey=DIC',
			onclick : function(view, node) {
				this.selectedNode = node;
				var parentId = node.data.id;
				var grid = Ext.getCmp('dicGrid');
				if (grid != null) {
					if (parentId == 0) {
						grid.setTitle('所有数据字典');
					} else {
						grid.setTitle(node.data.text + '--数据字典');
					}
					var store = grid.getStore();
					store.proxy.url = __ctxPath + "/system/listDictionary.do";
					store.proxy.extraParams = {
						parentId : parentId
					};
					store.reload();
				}
			},
			contextMenuItems : [{
						text : '新建分类',
						scope : this,
						iconCls : 'btn-add',
						handler : function() {
							var dicType = Ext.getCmp('dicType');
							var parentId = dicType.selectedNode.id;

							var globalTypeForm = new GlobalTypeForm({
										parentId : parentId,
										catKey : 'DIC',
										callback : function() {
											dicType.store.reload();
										}
									});
							globalTypeForm.show();
						}
					}, {
						text : '修改分类',
						scope : this,
						iconCls : 'btn-edit',
						handler : function() {
							var dicType = Ext.getCmp('dicType');
							var proTypeId = dicType.selectedNode.id;

							var globalTypeForm = new GlobalTypeForm({
										proTypeId : proTypeId,
										callback : function() {
											Ext.getCmp('dicType').store.reload();
										}
									});
							globalTypeForm.show();
						}
					}, {
						text : '添加字典项',
						scope : this,
						iconCls : 'btn-add',
						handler : this.addDic
					}]
		});

		this.topbar = new Ext.Toolbar({
					items : [{
								text : '添加数字字典',
								iconCls : 'btn-add',
								scope : this,
								handler : this.addDic
							}, '-', {
								xtype : 'button',
								text : '保存',
								iconCls : 'btn-save',
								scope : this,
								handler : this.saveDic
							}, '-', {
								text : '删除',
								iconCls : 'btn-del',
								scope : this,
								handler : this.delDic
							}, '-', {
								text : '转移类型',
								iconCls : 'btn-confApply-yes',
								scope : this,
								handler : this.transDic
							}]
				});
		this.centerPanel = new KE.GridPanel({
					region : 'center',
					tbar : this.topbar,
					id : 'dicGrid',
					url : __ctxPath + '/system/listDictionary.do',
					fields : ['dicId', 'itemName', 'itemValue', 'descp', 'sn',
							'globalType', 'parentDictName', 'code'],
					plugins : [new Ext.grid.plugin.CellEditing({
								clicksToEdit : 2
							})],
					columns : [{
								header : 'ID',
								dataIndex : 'dicId',
								hidden : true
//							}, {
//								header : 'parentId',
//								dataIndex : 'globalType',
//								hidden : true,
//								renderer : function(globalType) {
//									if (globalType) return globalType.parentId;
//									else return 0;
//								}
							}, {
								header : 'proTypeId',
								dataIndex : 'globalType',
								hidden : true,
								renderer : function(globalType) {
									if (globalType)
										return globalType.proTypeId;
								}
							}, {
								header : '分类名称',
								dataIndex : 'itemName'
							}, {
								header : '值',
								dataIndex : 'itemValue',
								editor : new Ext.form.TextField({
											allowBlank : false
										})
							}, {
								header : '代码',
								dataIndex : 'code',
								editor : new Ext.form.TextField()
//							}, {
//								header : '父值',
//								dataIndex : 'parentDictName'
							}, {
								header : '描述',
								dataIndex : 'descp',
								editor : new Ext.form.TextField()
							}, {
								header : '序号',
								dataIndex : 'sn',
								editor : new Ext.form.TextField()
							}],
					sorters : [{
								property : 'dicId',
								direction : 'desc'
							}]
				});

	},
	// 增加
	addDic : function() {
		var gridPanel = this.centerPanel;
		var dicType = Ext.getCmp('dicType');
		var selectedNode = dicType.selectedNode;

		var parentId = 0;
		if (selectedNode != null) {
			parentId = selectedNode.id;
		}
		if (parentId == 0) {
			Ext.ux.Toast.msg('操作信息', '请先从左侧树选择字典分类');
			return;
		}
		var typeName = dicType.selectedNode.data.text;
		new DictionaryForm({
					parentId : parentId,
					typeName : typeName,
					callback : function() {
						gridPanel.getStore().reload();
					}
				}).show();
	},
	// 保存
	saveDic : function() {
		var params = [];
		var grid = this.centerPanel;
		var store = grid.getStore();

		for (var i = 0; i < store.getCount(); i += 1) {
			var record = store.getAt(i);
			if (record.dirty) {
				params.push(record.data);
			}
		}

		if (params.length == 0) {
			Ext.ux.Toast.msg('信息', '没有对数据进行任何更改');
			return;
		}
		Ext.Ajax.request({
					method : 'post',
					url : __ctxPath + '/system/mulSaveDictionary.do',
					params : {
						data : Ext.encode(params)
					},
					success : function(request) {
						Ext.ux.Toast.msg('操作信息', '成功设置');
						store.reload();
						grid.getView().refresh();
					},
					failure : function(request) {
						Ext.ux.Toast.msg('操作信息', '设置出错，请联系管理员!');
					}
				});

	},
	// 删除
	delDic : function() {
		var gridPanel = this.centerPanel;
		var selectRecords = gridPanel.getSelectionModel().getSelection();
		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
			return;
		}
		var ids = Array();
		for (var i = 0; i < selectRecords.length; i++) {
			ids.push(selectRecords[i].data.dicId);
		}

		Ext.Msg.confirm('信息确认', '您确认要删除所选记录吗？', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
									url : __ctxPath
											+ '/system/multiDelDictionary.do',
									params : {
										ids : ids
									},
									method : 'POST',
									success : function(response, options) {
										Ext.ux.Toast.msg('操作信息', '成功删除该数字字典！');
										gridPanel.getStore().reload();
									},
									failure : function(response, options) {
										Ext.ux.Toast
												.msg('操作信息', '操作出错，请联系管理员！');
									}
								});
					}
				});
	},
	// 转移
	transDic : function() {
		var grid = Ext.getCmp('dicGrid');
		var rows = grid.getSelectionModel().getSelection();
		var dicIds = '';
		if (rows.length == 0) {
			Ext.ux.Toast.msg('操作信息', '请选择记录!');
			return;
		}
		for (var i = 0; i < rows.length; i++) {
			if (i > 0) {
				dicIds += ',';
			}
			dicIds += rows[i].data.dicId;
		}

		new DicTypeChangeWin({
					dicIds : dicIds,
					callback : function() {
						grid.getStore().reload();
					}
				}).show();
	}

});